﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChessSuite.Utilities.Common.Enumeations;

namespace ChessSuite.Utilities.Common.Utilities
{
    public static partial class PieceTypeUtility
    {
        public static PieceType Split(this PieceType first, PieceType second)
        {
            //Debug.Assert(first.Contains(second));
            if (!first.Contains(second))
                throw new ArgumentException("Unable to remove group of pieces from other group, when they're not contained therein.",
                    "second");

            PieceType res = PieceType.None;
            if (first.HasPawn() && !second.HasPawn()) res |= PieceType.Pawn;
            if (first.HasBishop() && !second.HasBishop()) res |= PieceType.Bishop;
            if (first.HasKnight() && !second.HasKnight()) res |= PieceType.Knight;
            if (first.HasRook() && !second.HasRook()) res |= PieceType.Rook;
            if (first.HasQueen() && !second.HasQueen()) res |= PieceType.Queen;
            if (first.HasKing() && !second.HasKing()) res |= PieceType.King;
            return res;
        }

        public static IEnumerable<PieceType> Split(this PieceType first, IEnumerable<PieceType> second)
        {
            //Debug.Assert(first.Contains(second));
            if (!first.Contains(second))
                throw new ArgumentException("Unable to remove group of pieces from other group, when they're not contained therein.",
                    "second");

            return FromIndividualCounts(first.Count(PieceType.Pawn) - second.Count(PieceType.Pawn),
                first.Count(PieceType.Bishop) - second.Count(PieceType.Bishop),
                first.Count(PieceType.Knight) - second.Count(PieceType.Knight),
                first.Count(PieceType.Rook) - second.Count(PieceType.Rook),
                first.Count(PieceType.Queen) - second.Count(PieceType.Queen),
                first.Count(PieceType.King) - second.Count(PieceType.King));
        }

        public static IEnumerable<PieceType> Split(this IEnumerable<PieceType> first, PieceType second)
        {
            //Debug.Assert(first.Contains(second));
            if (!first.Contains(second))
                throw new ArgumentException("Unable to remove group of pieces from other group, when they're not contained therein.",
                    "second");

            return FromIndividualCounts(first.Count(PieceType.Pawn) - second.Count(PieceType.Pawn),
                first.Count(PieceType.Bishop) - second.Count(PieceType.Bishop),
                first.Count(PieceType.Knight) - second.Count(PieceType.Knight),
                first.Count(PieceType.Rook) - second.Count(PieceType.Rook),
                first.Count(PieceType.Queen) - second.Count(PieceType.Queen),
                first.Count(PieceType.King) - second.Count(PieceType.King));
        }

        public static IEnumerable<PieceType> Split(this IEnumerable<PieceType> first, IEnumerable<PieceType> second)
        {
            //Debug.Assert(first.Contains(second));
            if (!first.Contains(second))
                throw new ArgumentException("Unable to remove group of pieces from other group, when they're not contained therein.",
                    "second");

            return FromIndividualCounts(first.Count(PieceType.Pawn) - second.Count(PieceType.Pawn),
                first.Count(PieceType.Bishop) - second.Count(PieceType.Bishop),
                first.Count(PieceType.Knight) - second.Count(PieceType.Knight),
                first.Count(PieceType.Rook) - second.Count(PieceType.Rook),
                first.Count(PieceType.Queen) - second.Count(PieceType.Queen),
                first.Count(PieceType.King) - second.Count(PieceType.King));
        }
    }
}
